package cn.edu.fzu.delivery.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Collections;

@Configuration
@EnableWebMvc
@EnableOpenApi
@EnableKnife4j
@ComponentScan(basePackages = "cn.edu.fzu.delivery.controller")
public class SwaggerConfig extends WebMvcConfigurationSupport {

    @Bean
    public Docket webApi() {
        return new Docket(DocumentationType.OAS_30).select()
                .apis(RequestHandlerSelectors.basePackage("cn.edu.fzu.delivery.controller"))
                .paths(PathSelectors.ant("/delivery-api-0.0.1-SNAPSHOT/api/**")).build().groupName("快递系统接口文档V1.0").pathMapping("/").enable(true)
                .apiInfo(apiInfo("快递系统接口文档V1.0", "文档中可以查询及测试接口调用参数和结果", "1.0"))
                .globalRequestParameters(Collections.singletonList(new springfox.documentation.builders.RequestParameterBuilder()
                                // 不能叫Authorization
                                .name("token")
                                .description("token")
                                .in(ParameterType.HEADER)
//                                .required(true)
                                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
                                .build()));
    }


    private ApiInfo apiInfo(String name, String description, String version) {
        return new ApiInfoBuilder().title(name).description(description).version(version).build();
    }

}
